@echo off
chcp 65001 >nul
REM AI Video Hub Windows 启动脚本

echo 🚀 启动 AI Video Hub...
echo.

REM 检查Python是否安装
python --version >nul 2>&1
if errorlevel 1 (
    echo ❌ Python 未找到，请先安装 Python
    pause
    exit /b 1
)

REM 检查是否已存在虚拟环境
if not exist ".venv" (
    echo 📦 创建虚拟环境...
    python -m venv .venv
)

REM 激活虚拟环境
echo 🔌 激活虚拟环境...
echo .venv\Scripts\activate.bat

REM 检查并安装依赖
if not exist ".requirements_installed" (
    echo 📥 安装项目依赖...
    pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple/
    pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
    if errorlevel 1 (
        echo ❌ 依赖安装失败
        pause
        exit /b 1
    )
    echo. > .requirements_installed
) else (
    REM 检查requirements.txt是否比标记文件新
    for /f %%i in ('dir /b /o:d requirements.txt .requirements_installed 2^>nul') do set "last=%%i"
    if "%last%"=="requirements.txt" (
        echo 📥 更新项目依赖...
        pip install -r requirements.txt
        echo. > .requirements_installed
    ) else (
        echo ✅ 依赖已安装，跳过安装步骤
    )
)

REM 检查环境变量文件
if not exist ".env" (
    echo ⚠️  警告: .env 文件不存在，创建示例文件...
    echo # AI Video Hub 环境变量配置 > .env
    echo DATABASE_URL=sqlite:///./aivideo.db >> .env
    echo REDIS_URL=redis://localhost:6379/0 >> .env
    echo OPENAI_API_KEY= >> .env
    echo ANTHROPIC_API_KEY= >> .env
    echo OLLAMA_BASE_URL=http://localhost:11434 >> .env
    echo VIDEO_OUTPUT_DIR=videos >> .env
    echo MAX_VIDEO_DURATION=300 >> .env
    echo MAX_FILE_SIZE=50000 >> .env
    echo 已创建 .env 示例文件，请根据需要填写配置
)

REM 创建必要的目录
echo 📁 创建项目目录...
if not exist "videos" mkdir videos
if not exist "logs" mkdir logs

echo.
echo 🎬 启动 AI Video Hub 服务...
echo 🌐 访问 http://127.0.0.1:8000 查看服务
echo 📋 API 文档: http://127.0.0.1:8000/docs
echo.

REM 启动服务
uvicorn main:app --host 0.0.0.0 --port 8000 --reload

pause
